<?php

namespace alipay;

use alipay\model\AlipayTradeAppPayContentBuilder;
use alipay\model\AlipayTradeFastpayRefundQueryContentBuilder;
use alipay\model\AlipayTradePagePayContentBuilder;
use alipay\model\AlipayTradeWapPayContentBuilder;
use alipay\model\AlipayTradeQueryContentBuilder;
use alipay\model\AlipayTradeRefundContentBuilder;

class  AliPayApi
{


    /**
     * APP支付请求
     * @param $out_trade_no 商户订单号，商户网站订单系统中唯一订单号，必填
     * @param $subject  订单名称，必填
     * @param $total_amount 付款金额，必填（单位元）
     * @param $body 商品描述，可空
     * @return mixed
     */
    public function appPay($out_trade_no, $subject, $total_amount, $body)
    {
        //构造参数
        $payRequestBuilder = new AlipayTradeAppPayContentBuilder();
        $payRequestBuilder->setBody($body);
        if (empty($subject)) {
            $subject = AliPayConfig::$config['subject'];
        }
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setOutTradeNo($out_trade_no);
        $config = AliPayConfig::$config;
        $aop = new AlipayTradeService();
        $response = $aop->appPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
        //输出表单
        return $response;
    }

    /**
     * 手机网站支付请求
     * @param $out_trade_no 商户订单号，商户网站订单系统中唯一订单号，必填
     * @param $subject  订单名称，必填
     * @param $total_amount 付款金额，必填（单位元）
     * @param $body 商品描述，可空
     * @return mixed
     */
    public function wapPay($out_trade_no, $subject, $total_amount, $body)
    {
        //构造参数
        $payRequestBuilder = new AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setBody($body);
        if (empty($subject)) {
            $subject = AliPayConfig::$config['subject'];
        }
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setOutTradeNo($out_trade_no);
        $config = AliPayConfig::$config;
        $aop = new AlipayTradeService();
        $response = $aop->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
        //输出表单
        return $response;
    }

    /**
     * 验证回调数据
     * @param $data
     * @return bool true 校验成功 false校验失败
     */
    public function check($data)
    {
        $alipay_sevice = new AlipayTradeService();
        $result = $alipay_sevice->check($data);
        return $result;
    }

    /**
     * 电脑网站支付请求
     * @param $out_trade_no 商户订单号，商户网站订单系统中唯一订单号，必填
     * @param $subject  订单名称，必填
     * @param $total_amount 付款金额，必填（单位元）
     * @param $body 商品描述，可空
     * @return mixed
     */
    public function pagePay($out_trade_no, $subject, $total_amount, $body)
    {
        //构造参数
        $payRequestBuilder = new AlipayTradePagePayContentBuilder();
        $payRequestBuilder->setBody($body);
        if (empty($subject)) {
            $subject = AliPayConfig::$config['subject'];
        }
        $payRequestBuilder->setSubject($subject);
        $payRequestBuilder->setTotalAmount($total_amount);
        $payRequestBuilder->setOutTradeNo($out_trade_no);
        $config = AliPayConfig::$config;
        $aop = new AlipayTradeService();
        $response = $aop->pagePay($payRequestBuilder, $config['return_url'], $config['notify_url']);
        //输出表单
        return $response;
    }


    /**
     * 统一收单线下交易查询
     * @param string $out_trade_no 商户订单号，商户网站订单系统中唯一订单号
     * @return mixed
     */
    public function query($out_trade_no = '')
    {
        //请二选一设置
        //构造参数
        $RequestBuilder = new AlipayTradeQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setTradeNo('');
        $aop = new AlipayTradeService();
        $response = $aop->Query($RequestBuilder);
        if (isset($response->trade_status) && $response->trade_status === "TRADE_SUCCESS") {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 统一收单交易退款接口
     * @param $out_trade_no 商户订单号，商户网站订单系统中唯一订单号
     * @param $refund_amount 需要退款的金额，该金额不能大于订单金额，必填
     * @param $refund_reason 退款的原因说明
     * @param string $out_request_no 标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
     * @return mixed
     */
    public function return_pay($out_trade_no, $refund_amount, $refund_reason, $out_request_no = '1')
    {
        //构造参数
        $RequestBuilder = new AlipayTradeRefundContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setTradeNo('');
        $RequestBuilder->setRefundAmount($refund_amount);
        $RequestBuilder->setOutRequestNo($out_request_no);
        $RequestBuilder->setRefundReason($refund_reason);
        $aop = new AlipayTradeService();
        $response = $aop->Refund($RequestBuilder);
        return $response;
    }

    /**
     * 统一收单交易退款查询
     * @param $out_trade_no 商户订单号，商户网站订单系统中唯一订单号
     * @return mixed
     */
    public function refund_query($out_trade_no)
    {
        //请求退款接口时，传入的退款请求号，如果在退款请求时未传入，则该值为创建交易时的外部交易号，必填
        $out_request_no = str_replace('pay', 'refund', $out_trade_no);
        //构造参数
        $RequestBuilder = new AlipayTradeFastpayRefundQueryContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setTradeNo('');
        $RequestBuilder->setOutRequestNo($out_request_no);
        $aop = new AlipayTradeService();
        $response = $aop->refundQuery($RequestBuilder);
        return $response;
    }
}

?>